package com.asa.common.net

import com.asa.common.common.LogUtils
import okhttp3.FormBody
import okhttp3.Interceptor
import okhttp3.Response

class NetLogInterceptor:Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request();
        val startTime = System.currentTimeMillis();
        val response = chain.proceed(chain.request());
        val endTime = System.currentTimeMillis();
        val duration = endTime - startTime;
        val mediaType = response.body?.contentType();
        val content = response.body?.string();

        log("\n")
        log("----------Start----------------");
        log("| $request");
        val method = request.method;
        if ("POST" == method) {
            val sb = StringBuilder();
            if (request.body is FormBody) {
                val body = request.body as FormBody
                for (i in 0 until body.size) {
                    sb.append(body.encodedName(i) + "=" + body.encodedValue(i) + ",");
                }
                sb.delete(sb.length - 1, sb.length);
                log("| RequestParams:{" + sb.toString() + "}");
            }
        }
        log("| Response:" + content);
        log("----------End:" + duration + "毫秒----------");
        return response.newBuilder()
            .body(okhttp3.ResponseBody.create(mediaType, content ?: ""))
            .build();
    }

    private fun log(content:String){
        LogUtils.d(content)
    }


}